ALTER               FUNCTION fn_shipped_orders_by_month_for_fcbl_report(
  
  @CUSTOMER_INPUT VARCHAR(100),
  @PRODUCT_INPUT VARCHAR(100),
  @SALES_PERSON_INPUT VARCHAR(100),   -- Added by Gandhi on 10th Sep 2004		
  @MONTH_FROM INT,
  @MONTH_TO INT,
  @YEAR_INPUT INT )
  
  
  RETURNS @FN_RESULTS TABLE( CUSTOMER VARCHAR(100), PRODUCT VARCHAR(100), SALES_PERSON VARCHAR(100), END_USER VARCHAR(100), MONTH INT, YEAR INT, QUANTITY INT, UNIT_PRICE FLOAT )
    
  AS
  BEGIN
  	DECLARE @OLD_PRICE FLOAT

	DECLARE @MKTG_PART_NUMBER VARCHAR(100) 
 	
 	DECLARE @TEMP_CURSOR CURSOR 

	IF RTRIM(@MONTH_FROM) = -1
	BEGIN
		SET @MONTH_FROM = 0
	END

	IF RTRIM(@MONTH_TO) = -1
	BEGIN
		SET @MONTH_TO = 11
	END
	
	DECLARE @TO_MONTH INT	
	SET @TO_MONTH = @MONTH_FROM + @MONTH_TO 

	DECLARE @YEAR_TO INT
	
	
	SET @YEAR_TO = @YEAR_INPUT + @TO_MONTH/12 
	SET @MONTH_TO = @TO_MONTH%12	

  
  	IF @CUSTOMER_INPUT IS NULL OR RTRIM( @CUSTOMER_INPUT ) = ''
  	BEGIN
  		SET @CUSTOMER_INPUT = ''
  	END
  	

  	IF @CUSTOMER_INPUT = '0' AND @PRODUCT_INPUT = '0'
  	BEGIN
  		
		IF( @SALES_PERSON_INPUT = '-1')
		BEGIN
			SET @TEMP_CURSOR = CURSOR FOR
	  	    	SELECT CUST.C_NAME[CUSTOMER], P.PART_NUMBER[PRODUCT], SO.SO_SALES_PERSON[SALES_PERSON], 
                        --SO.SO_ENDUSER[END_USER],
			CASE 
			WHEN ( SO_ODM_ID IS NULL OR SO_ODM_ID < 0 ) THEN ''
			ELSE ( SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = SO_ODM_ID )
			END AS END_USER, 
			DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE )-1, 
	  		DATEPART( YEAR, CSLIT.CSLIT_SHIPPED_DATE ),CSLIT.CSLIT_QTY_SHIPPED[QUANTITY], COOL.COOL_UNIT_COST[UNIT_PRICE]
	  		FROM CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT, 
	  		CUSTOMER_OPEN_ORDER_LINEITEM COOL, PRODUCT P, SALES_ORDER SO, CUSTOMER CUST  
	  		WHERE ( ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 >= @MONTH_FROM AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_INPUT AND ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 < @TO_MONTH ) OR
	  		      ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 < @MONTH_TO AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_TO AND @YEAR_INPUT <> @YEAR_TO ) OR
	  		      ( DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) < @YEAR_TO AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) > @YEAR_INPUT ) ) 
	 				
	  		AND CSLIT.CSLIT_STATUS = 'SHIPPED'
	 		AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID
	  		AND CSLIT.LINEITEM_ID = COOL.LINEITEM_ID
	  		AND COOL.PRODUCT_ID = P.PRODUCT_ID
	  		AND COOL.SALESORDER_ID = SO.SALESORDER_ID
	 		AND SO.CUSTOMER_ID = CUST.CUSTOMER_ID
			and SO.SO_Is_Sample = 1
		END
		ELSE   -- Added by Gandhi on 14th Sep 2004
		BEGIN
			SET @TEMP_CURSOR = CURSOR FOR
	  	    	SELECT CUST.C_NAME[CUSTOMER], P.PART_NUMBER[PRODUCT], SO.SO_SALES_PERSON[SALES_PERSON],
                        --SO.SO_ENDUSER[END_USER], 
			CASE 
			WHEN ( SO_ODM_ID IS NULL OR SO_ODM_ID < 0 ) THEN ''
			ELSE ( SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = SO_ODM_ID )
			END AS END_USER, 
                        DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE )-1, 
	  		DATEPART( YEAR, CSLIT.CSLIT_SHIPPED_DATE ),CSLIT.CSLIT_QTY_SHIPPED[QUANTITY], COOL.COOL_UNIT_COST[UNIT_PRICE]
	  		FROM CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT, 
	  		CUSTOMER_OPEN_ORDER_LINEITEM COOL, PRODUCT P, SALES_ORDER SO, CUSTOMER CUST  
	  		WHERE ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 >= @MONTH_FROM AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_INPUT AND ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 <= @TO_MONTH )
	 				
	  		AND CSLIT.CSLIT_STATUS = 'SHIPPED'
	 		AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID
	  		AND CSLIT.LINEITEM_ID = COOL.LINEITEM_ID
	  		AND COOL.PRODUCT_ID = P.PRODUCT_ID
	  		AND COOL.SALESORDER_ID = SO.SALESORDER_ID
	 		AND SO.CUSTOMER_ID = CUST.CUSTOMER_ID
			and SO.SO_Is_Sample = 1
		END
			 
 	END	
  	ELSE IF @CUSTOMER_INPUT = '0' AND @PRODUCT_INPUT <> '0'
  	BEGIN
  		
		IF( @SALES_PERSON_INPUT = '-1')
		BEGIN
			SET @TEMP_CURSOR = CURSOR FOR
	  	    	SELECT CUST.C_NAME[CUSTOMER], P.PART_NUMBER[PRODUCT], SO.SO_SALES_PERSON[SALES_PERSON], 
                        --SO.SO_ENDUSER[END_USER], 
			CASE 
			WHEN ( SO_ODM_ID IS NULL OR SO_ODM_ID < 0 ) THEN ''
			ELSE ( SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = SO_ODM_ID )
			END AS END_USER, 
                        DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE )-1, 
	  		DATEPART( YEAR, CSLIT.CSLIT_SHIPPED_DATE ),CSLIT.CSLIT_QTY_SHIPPED[QUANTITY], COOL.COOL_UNIT_COST[UNIT_PRICE]
	  		FROM CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT, 
	  		CUSTOMER_OPEN_ORDER_LINEITEM COOL, PRODUCT P, SALES_ORDER SO, CUSTOMER CUST  
	  		WHERE   P.PART_NUMBER = @PRODUCT_INPUT 		
	  		AND   ( ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 >= @MONTH_FROM AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_INPUT AND ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 < @TO_MONTH ) OR
	  		      ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 < @MONTH_TO AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_TO AND @YEAR_INPUT <> @YEAR_TO ) OR
	  		      ( DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) < @YEAR_TO AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) > @YEAR_INPUT ) ) 
	 				
	  		AND CSLIT.CSLIT_STATUS = 'SHIPPED'
	 		AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID
	  		AND CSLIT.LINEITEM_ID = COOL.LINEITEM_ID
	  		AND COOL.PRODUCT_ID = P.PRODUCT_ID
	  		AND COOL.SALESORDER_ID = SO.SALESORDER_ID
	 		AND SO.CUSTOMER_ID = CUST.CUSTOMER_ID
			and SO.SO_Is_Sample = 1
		END
		ELSE	-- Added by Gandhi on 14th Sep 2004
		BEGIN
			SET @TEMP_CURSOR = CURSOR FOR
	  	    	SELECT CUST.C_NAME[CUSTOMER], P.PART_NUMBER[PRODUCT], SO.SO_SALES_PERSON[SALES_PERSON], 
                        --SO.SO_ENDUSER[END_USER], 
			CASE 
			WHEN ( SO_ODM_ID IS NULL OR SO_ODM_ID < 0 ) THEN ''
			ELSE ( SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = SO_ODM_ID )
			END AS END_USER,  
                        DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE )-1, 
	  		DATEPART( YEAR, CSLIT.CSLIT_SHIPPED_DATE ),CSLIT.CSLIT_QTY_SHIPPED[QUANTITY], COOL.COOL_UNIT_COST[UNIT_PRICE]
	  		FROM CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT, 
	  		CUSTOMER_OPEN_ORDER_LINEITEM COOL, PRODUCT P, SALES_ORDER SO, CUSTOMER CUST  
	  		WHERE   P.PART_NUMBER = @PRODUCT_INPUT 		
	  		AND   ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 >= @MONTH_FROM AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_INPUT AND ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 <= @TO_MONTH )	 				
	  		AND CSLIT.CSLIT_STATUS = 'SHIPPED'
	 		AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID
	  		AND CSLIT.LINEITEM_ID = COOL.LINEITEM_ID
	  		AND COOL.PRODUCT_ID = P.PRODUCT_ID
	  		AND COOL.SALESORDER_ID = SO.SALESORDER_ID
	 		AND SO.CUSTOMER_ID = CUST.CUSTOMER_ID
			and SO.SO_Is_Sample = 1
		END 
 	END
  	ELSE IF @CUSTOMER_INPUT <> '0' AND @PRODUCT_INPUT = '0'
  	BEGIN
  		
		IF( @SALES_PERSON_INPUT = '-1')
		BEGIN
			SET @TEMP_CURSOR = CURSOR FOR
	  	    	SELECT CUST.C_NAME[CUSTOMER], P.PART_NUMBER[PRODUCT], SO.SO_SALES_PERSON[SALES_PERSON], 
                        --SO.SO_ENDUSER[END_USER], 
			CASE 
			WHEN ( SO_ODM_ID IS NULL OR SO_ODM_ID < 0 ) THEN ''
			ELSE ( SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = SO_ODM_ID )
			END AS END_USER, 

                        DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE )-1, 
	  		DATEPART( YEAR, CSLIT.CSLIT_SHIPPED_DATE ),CSLIT.CSLIT_QTY_SHIPPED[QUANTITY], COOL.COOL_UNIT_COST[UNIT_PRICE]
	  		FROM CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT, 
	  		CUSTOMER_OPEN_ORDER_LINEITEM COOL, PRODUCT P, SALES_ORDER SO, CUSTOMER CUST  
	  		WHERE  CUST.C_NAME = @CUSTOMER_INPUT
	  		AND   ( ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 >= @MONTH_FROM AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_INPUT AND ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 < @TO_MONTH ) OR
	  		      ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1  < @MONTH_TO AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_TO AND @YEAR_INPUT <> @YEAR_TO ) OR
	  		      ( DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) < @YEAR_TO AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) > @YEAR_INPUT ) ) 
	 				
	  		AND CSLIT.CSLIT_STATUS = 'SHIPPED'
	 		AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID
	  		AND CSLIT.LINEITEM_ID = COOL.LINEITEM_ID
	  		AND COOL.PRODUCT_ID = P.PRODUCT_ID
	  		AND COOL.SALESORDER_ID = SO.SALESORDER_ID
	 		AND SO.CUSTOMER_ID = CUST.CUSTOMER_ID
			and SO.SO_Is_Sample = 1
		END
		ELSE	-- Added by Gandhi on 14th Sep 2004
		BEGIN
			SET @TEMP_CURSOR = CURSOR FOR
	  	    	SELECT CUST.C_NAME[CUSTOMER], P.PART_NUMBER[PRODUCT], SO.SO_SALES_PERSON[SALES_PERSON], 
                        --SO.SO_ENDUSER[END_USER], 
			CASE 
			WHEN ( SO_ODM_ID IS NULL OR SO_ODM_ID < 0 ) THEN ''
			ELSE ( SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = SO_ODM_ID )
			END AS END_USER, 

				DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE )-1, 
	  		DATEPART( YEAR, CSLIT.CSLIT_SHIPPED_DATE ),CSLIT.CSLIT_QTY_SHIPPED[QUANTITY], COOL.COOL_UNIT_COST[UNIT_PRICE]
	  		FROM CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT, 
	  		CUSTOMER_OPEN_ORDER_LINEITEM COOL, PRODUCT P, SALES_ORDER SO, CUSTOMER CUST  
	  		WHERE  CUST.C_NAME = @CUSTOMER_INPUT
	  		AND   ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 >= @MONTH_FROM AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_INPUT AND ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 <= @TO_MONTH )		 				
	  		AND CSLIT.CSLIT_STATUS = 'SHIPPED'
	 		AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID
	  		AND CSLIT.LINEITEM_ID = COOL.LINEITEM_ID
	  		AND COOL.PRODUCT_ID = P.PRODUCT_ID
	  		AND COOL.SALESORDER_ID = SO.SALESORDER_ID
	 		AND SO.CUSTOMER_ID = CUST.CUSTOMER_ID
			and SO.SO_Is_Sample = 1
		END 
 	END
  	ELSE IF @CUSTOMER_INPUT <> '0' AND @PRODUCT_INPUT <> '0'
  	BEGIN
  		
		IF( @SALES_PERSON_INPUT = '-1')
		BEGIN
			SET @TEMP_CURSOR = CURSOR FOR
	  	    	SELECT CUST.C_NAME[CUSTOMER], P.PART_NUMBER[PRODUCT], SO.SO_SALES_PERSON[SALES_PERSON], 
                        --SO.SO_ENDUSER[END_USER], 
			CASE 
			WHEN ( SO_ODM_ID IS NULL OR SO_ODM_ID < 0 ) THEN ''
			ELSE ( SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = SO_ODM_ID )
			END AS END_USER, 
                        DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE )-1, 
	  		DATEPART( YEAR, CSLIT.CSLIT_SHIPPED_DATE ),CSLIT.CSLIT_QTY_SHIPPED[QUANTITY], COOL.COOL_UNIT_COST[UNIT_PRICE]
	  		FROM CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT, 
	  		CUSTOMER_OPEN_ORDER_LINEITEM COOL, PRODUCT P, SALES_ORDER SO, CUSTOMER CUST  
	  		WHERE  CUST.C_NAME = @CUSTOMER_INPUT
	  		AND P.PART_NUMBER = @PRODUCT_INPUT 		
	  		AND   ( ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 >= @MONTH_FROM AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_INPUT AND ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 < @TO_MONTH ) OR
	  		      ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 < @MONTH_TO AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_TO AND @YEAR_INPUT <> @YEAR_TO ) OR
	  		      ( DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) < @YEAR_TO AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) > @YEAR_INPUT ) ) 
	 				
	  		AND CSLIT.CSLIT_STATUS = 'SHIPPED'
	 		AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID
	  		AND CSLIT.LINEITEM_ID = COOL.LINEITEM_ID
	  		AND COOL.PRODUCT_ID = P.PRODUCT_ID
	  		AND COOL.SALESORDER_ID = SO.SALESORDER_ID
	 		AND SO.CUSTOMER_ID = CUST.CUSTOMER_ID
			and SO.SO_Is_Sample = 1
		END 
		ELSE	-- Added by Gandhi on 14th Sep 2004
		BEGIN
			SET @TEMP_CURSOR = CURSOR FOR
	  	    	SELECT CUST.C_NAME[CUSTOMER], P.PART_NUMBER[PRODUCT], SO.SO_SALES_PERSON[SALES_PERSON], 
			--SO.SO_ENDUSER[END_USER], 
			CASE 
			WHEN ( SO_ODM_ID IS NULL OR SO_ODM_ID < 0 ) THEN ''
			ELSE ( SELECT C_NAME FROM CUSTOMER WHERE CUSTOMER_ID = SO_ODM_ID )
			END AS END_USER, 
			DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE )-1, 
	  		DATEPART( YEAR, CSLIT.CSLIT_SHIPPED_DATE ),CSLIT.CSLIT_QTY_SHIPPED[QUANTITY], COOL.COOL_UNIT_COST[UNIT_PRICE]
	  		FROM CUSTOMER_SHIPPING_TABLE CST, CUSTOMER_SHIPPING_LINE_ITEM_TABLE CSLIT, 
	  		CUSTOMER_OPEN_ORDER_LINEITEM COOL, PRODUCT P, SALES_ORDER SO, CUSTOMER CUST  
	  		WHERE  CUST.C_NAME = @CUSTOMER_INPUT
	  		AND P.PART_NUMBER = @PRODUCT_INPUT 		
	  		AND   ( ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 >= @MONTH_FROM AND DATEPART( YEAR,  CSLIT.CSLIT_SHIPPED_DATE ) = @YEAR_INPUT AND ( DATEPART( MONTH, CSLIT.CSLIT_SHIPPED_DATE ))-1 <= @TO_MONTH )		 				
	  		AND CSLIT.CSLIT_STATUS = 'SHIPPED'
	 		AND CST.CUSTOMERSHIPPING_ID = CSLIT.CUSTOMERSHIPPING_ID
	  		AND CSLIT.LINEITEM_ID = COOL.LINEITEM_ID
	  		AND COOL.PRODUCT_ID = P.PRODUCT_ID
	  		AND COOL.SALESORDER_ID = SO.SALESORDER_ID
	 		AND SO.CUSTOMER_ID = CUST.CUSTOMER_ID
			and SO.SO_Is_Sample = 1
		END
 	END
 	
 
  	DECLARE @CUSTOMER VARCHAR(100)
  	DECLARE @PRODUCT VARCHAR(100)
	DECLARE @SALES_PERSON VARCHAR(100)
	DECLARE @END_USER VARCHAR(100)
  	DECLARE @MONTH INT
  	DECLARE @YEAR INT
  	DECLARE @QUANTITY INT
  	DECLARE @OLD_QUANTITY INT
  	DECLARE @UNIT_PRICE FLOAT
  	DECLARE @ROW_COUNT INT
  	
	IF( @SALES_PERSON_INPUT = '-1')  -- This block for Forecast Vs. Backlog Vs. Shipped Report
	BEGIN
	  	OPEN @TEMP_CURSOR 
	  		FETCH NEXT FROM @TEMP_CURSOR
	  		INTO @CUSTOMER, @PRODUCT, @SALES_PERSON, @END_USER, @MONTH, @YEAR, @QUANTITY, @UNIT_PRICE
	  		
	  		WHILE @@FETCH_STATUS = 0
	  		BEGIN
	  			SELECT @ROW_COUNT = COUNT(*) FROM @FN_RESULTS 
	  			WHERE CUSTOMER = @CUSTOMER AND PRODUCT = @PRODUCT AND MONTH = @MONTH AND YEAR = @YEAR 
	  				
	  			IF @ROW_COUNT = 0
	  			BEGIN
	  				INSERT INTO @FN_RESULTS VALUES ( @CUSTOMER, @PRODUCT, @SALES_PERSON, @END_USER, @MONTH, @YEAR, @QUANTITY, @QUANTITY * @UNIT_PRICE)
	  			END
	  
	  			ELSE
	  			BEGIN
	  				SELECT @OLD_QUANTITY = QUANTITY, @OLD_PRICE = UNIT_PRICE FROM @FN_RESULTS
	  				WHERE CUSTOMER = @CUSTOMER AND PRODUCT = @PRODUCT AND MONTH = @MONTH AND YEAR = @YEAR
	  
	  				UPDATE @FN_RESULTS SET QUANTITY = (@QUANTITY + @OLD_QUANTITY) , UNIT_PRICE = @OLD_PRICE +  ( @QUANTITY * @UNIT_PRICE )
	  				WHERE CUSTOMER = @CUSTOMER AND PRODUCT = @PRODUCT AND MONTH = @MONTH AND YEAR = @YEAR
	  			END	
			
				SELECT @MKTG_PART_NUMBER = Product.P_Marketing_part_Number from product,Process_Step  where Product.Part_Number = @PRODUCT and product.ProcessStep_ID in (select processstep_id from Process_Step where PS_Type_Name like 'Finished Goods') and product.processStep_id=Process_Step.ProcessStep_ID  
				UPDATE @FN_RESULTS SET PRODUCT =  @MKTG_PART_NUMBER WHERE PRODUCT = @PRODUCT		
	  			
	  			FETCH NEXT FROM @TEMP_CURSOR
	  			INTO  @CUSTOMER, @PRODUCT, @SALES_PERSON, @END_USER, @MONTH, @YEAR, @QUANTITY, @UNIT_PRICE
	  		END

		CLOSE @TEMP_CURSOR
	  	DEALLOCATE @TEMP_CURSOR

  	END
	
	ELSE  -- Added by Gandhi on 10th Sep 2004 for Forecast Vs. Actual Report
	BEGIN
	  	OPEN @TEMP_CURSOR 
	  		FETCH NEXT FROM @TEMP_CURSOR
	  		INTO @CUSTOMER, @PRODUCT, @SALES_PERSON, @END_USER, @MONTH, @YEAR, @QUANTITY, @UNIT_PRICE
	  		
	  		WHILE @@FETCH_STATUS = 0
	  		BEGIN
	  			SELECT @ROW_COUNT = COUNT(*) FROM @FN_RESULTS 
	  			WHERE CUSTOMER = @CUSTOMER AND PRODUCT = @PRODUCT AND MONTH = @MONTH AND YEAR = @YEAR AND SALES_PERSON = @SALES_PERSON AND END_USER =  @END_USER
	  				
	  			IF @ROW_COUNT = 0
	  			BEGIN
	  				INSERT INTO @FN_RESULTS VALUES ( @CUSTOMER, @PRODUCT, @SALES_PERSON, @END_USER, @MONTH, @YEAR, @QUANTITY, @UNIT_PRICE)
	  			END
	  
	  			ELSE
	  			BEGIN
	  				SELECT @OLD_QUANTITY = QUANTITY FROM @FN_RESULTS
	  				WHERE CUSTOMER = @CUSTOMER AND PRODUCT = @PRODUCT AND MONTH = @MONTH AND YEAR = @YEAR AND SALES_PERSON = @SALES_PERSON AND END_USER =  @END_USER
	  
	  				UPDATE @FN_RESULTS SET QUANTITY = (@QUANTITY + @OLD_QUANTITY)
	  				WHERE CUSTOMER = @CUSTOMER AND PRODUCT = @PRODUCT AND MONTH = @MONTH AND YEAR = @YEAR AND SALES_PERSON = @SALES_PERSON AND END_USER =  @END_USER
	  			END	
			
				SELECT @MKTG_PART_NUMBER = Product.P_Marketing_part_Number from product,Process_Step  where Product.Part_Number = @PRODUCT and product.ProcessStep_ID in (select processstep_id from Process_Step where PS_Type_Name like 'Finished Goods') and product.processStep_id=Process_Step.ProcessStep_ID  
				UPDATE @FN_RESULTS SET PRODUCT =  @MKTG_PART_NUMBER WHERE PRODUCT = @PRODUCT		
	  			
	  			FETCH NEXT FROM @TEMP_CURSOR
	  			INTO  @CUSTOMER, @PRODUCT, @SALES_PERSON, @END_USER, @MONTH, @YEAR, @QUANTITY, @UNIT_PRICE
	  		END
  		 
	 	CLOSE @TEMP_CURSOR
	  	DEALLOCATE @TEMP_CURSOR
	   
	END

  	RETURN
  END
 
